package com.gwt.dispatcher.server;

import javax.servlet.http.HttpSession;

import com.gwt.dispatcher.dispatcher.server.ActionHandler;
import com.gwt.dispatcher.dispatcher.shared.exceptions.DispatchException;
import com.gwt.dispatcher.shared.IncrementCounterAction;
import com.gwt.dispatcher.shared.IncrementCounterResult;

/**
 * Handler 
 * @author Cristian
 */
public class IncrementCounterHandler extends ActionHandler<IncrementCounterAction, IncrementCounterResult> {

    private int current = 0;

    @Override
    public IncrementCounterResult execute(IncrementCounterAction action) throws DispatchException {
      current += action.getAmount();
      
      HttpSession session = this.getThreadLocalRequest().getSession();
      if(session.getAttribute("userName") != null){
        session.getAttribute("userName");
      }else{
        session.setAttribute("userName", "cris");
      }
      
      return new IncrementCounterResult( action.getAmount(), current );
    }

}